﻿namespace rtgen_guiclient
{
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Windows.Forms;

    public class MSIDeploy : Form
    {
        private Button btnClose;
        private Button btnInstall;
        public CheckBox cbCredentials;
        private IContainer components;
        private ColumnHeader Computer;
        private Label label1;
        private Label label2;
        private Label label3;
        private Label label4;
        private Label label5;
        private ListView lvComputers;
        private ColumnHeader Status;
        public TextBox txtComputername;
        public TextBox txtDomain;
        private TextBox txtMSI;
        public TextBox txtPassword;
        public TextBox txtUsername;

        public MSIDeploy()
        {
            this.InitializeComponent();
        }

        private void btnInstall_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < this.txtComputername.Lines.Length; i++)
            {
                this.lvComputers.Items.Add(new ListViewItem(new string[] { this.txtComputername.Lines[i], "not started" }));
            }
            this.lvComputers.Update();
            for (int j = 0; j < this.txtComputername.Lines.Length; j++)
            {
                this.lvComputers.Items[j].SubItems[1].Text = "Installing...";
                this.lvComputers.Update();
                try
                {
                    this.InstallPackage(this.txtMSI.Text, this.txtComputername.Lines[j], this.txtDomain.Text, this.txtUsername.Text, this.txtPassword.Text, TimeSpan.FromSeconds(10.0));
                    this.lvComputers.Items[j].SubItems[1].Text = "Installed";
                }
                catch (Exception exception)
                {
                    this.lvComputers.Items[j].SubItems[1].Text = "Failed: " + exception.Message;
                }
                this.lvComputers.Update();
            }
        }

        private void cbCredentials_CheckedChanged(object sender, EventArgs e)
        {
            if (this.cbCredentials.Checked)
            {
                this.txtUsername.ReadOnly = true;
                this.txtPassword.ReadOnly = true;
                this.txtDomain.ReadOnly = true;
            }
            else
            {
                this.txtUsername.ReadOnly = false;
                this.txtPassword.ReadOnly = false;
                this.txtDomain.ReadOnly = false;
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            this.label4 = new Label();
            this.label3 = new Label();
            this.label2 = new Label();
            this.txtDomain = new TextBox();
            this.txtPassword = new TextBox();
            this.txtUsername = new TextBox();
            this.cbCredentials = new CheckBox();
            this.btnInstall = new Button();
            this.label1 = new Label();
            this.txtComputername = new TextBox();
            this.txtMSI = new TextBox();
            this.label5 = new Label();
            this.lvComputers = new ListView();
            this.Computer = new ColumnHeader();
            this.Status = new ColumnHeader();
            this.btnClose = new Button();
            base.SuspendLayout();
            this.label4.AutoSize = true;
            this.label4.Location = new Point(0x69, 0x135);
            this.label4.Name = "label4";
            this.label4.Size = new Size(0x2b, 13);
            this.label4.TabIndex = 0x15;
            this.label4.Text = "Domain";
            this.label3.AutoSize = true;
            this.label3.Location = new Point(0x69, 0x11a);
            this.label3.Name = "label3";
            this.label3.Size = new Size(0x35, 13);
            this.label3.TabIndex = 20;
            this.label3.Text = "Password";
            this.label2.AutoSize = true;
            this.label2.Location = new Point(0x69, 0xff);
            this.label2.Name = "label2";
            this.label2.Size = new Size(0x37, 13);
            this.label2.TabIndex = 0x13;
            this.label2.Text = "Username";
            this.txtDomain.Location = new Point(170, 0x12e);
            this.txtDomain.Name = "txtDomain";
            this.txtDomain.ReadOnly = true;
            this.txtDomain.Size = new Size(100, 20);
            this.txtDomain.TabIndex = 0x12;
            this.txtPassword.Location = new Point(170, 0x114);
            this.txtPassword.Name = "txtPassword";
            this.txtPassword.ReadOnly = true;
            this.txtPassword.Size = new Size(100, 20);
            this.txtPassword.TabIndex = 0x11;
            this.txtUsername.Location = new Point(170, 0xf8);
            this.txtUsername.Name = "txtUsername";
            this.txtUsername.ReadOnly = true;
            this.txtUsername.Size = new Size(100, 20);
            this.txtUsername.TabIndex = 0x10;
            this.cbCredentials.AutoSize = true;
            this.cbCredentials.Checked = true;
            this.cbCredentials.CheckState = CheckState.Checked;
            this.cbCredentials.Location = new Point(0x83, 0xe1);
            this.cbCredentials.Name = "cbCredentials";
            this.cbCredentials.Size = new Size(0x86, 0x11);
            this.cbCredentials.TabIndex = 15;
            this.cbCredentials.Text = "Use default credentials";
            this.cbCredentials.UseVisualStyleBackColor = true;
            this.cbCredentials.CheckedChanged += new EventHandler(this.cbCredentials_CheckedChanged);
            this.btnInstall.Location = new Point(0x83, 340);
            this.btnInstall.Name = "btnInstall";
            this.btnInstall.Size = new Size(0x4b, 0x17);
            this.btnInstall.TabIndex = 13;
            this.btnInstall.Text = "Install";
            this.btnInstall.UseVisualStyleBackColor = true;
            this.btnInstall.Click += new EventHandler(this.btnInstall_Click);
            this.label1.AutoSize = true;
            this.label1.Location = new Point(0x16, 0x61);
            this.label1.Name = "label1";
            this.label1.Size = new Size(0x67, 13);
            this.label1.TabIndex = 12;
            this.label1.Text = "Computername or IP";
            this.txtComputername.Location = new Point(0x83, 0x31);
            this.txtComputername.Multiline = true;
            this.txtComputername.Name = "txtComputername";
            this.txtComputername.Size = new Size(0x8b, 0x9e);
            this.txtComputername.TabIndex = 11;
            this.txtMSI.Location = new Point(0x83, 12);
            this.txtMSI.Name = "txtMSI";
            this.txtMSI.Size = new Size(0x8b, 20);
            this.txtMSI.TabIndex = 0x16;
            this.label5.AutoSize = true;
            this.label5.Location = new Point(0x11, 15);
            this.label5.Name = "label5";
            this.label5.Size = new Size(0x6c, 13);
            this.label5.TabIndex = 0x17;
            this.label5.Text = "Path to MSI package";
            this.lvComputers.Columns.AddRange(new ColumnHeader[] { this.Computer, this.Status });
            this.lvComputers.Location = new Point(12, 380);
            this.lvComputers.Name = "lvComputers";
            this.lvComputers.Size = new Size(0x10a, 0x7d);
            this.lvComputers.TabIndex = 0x18;
            this.lvComputers.UseCompatibleStateImageBehavior = false;
            this.lvComputers.View = View.Details;
            this.Computer.Text = "Computer";
            this.Computer.Width = 0x98;
            this.Status.Text = "Status";
            this.Status.Width = 0x67;
            this.btnClose.DialogResult = DialogResult.OK;
            this.btnClose.Location = new Point(0xd4, 340);
            this.btnClose.Name = "btnClose";
            this.btnClose.Size = new Size(0x4b, 0x17);
            this.btnClose.TabIndex = 0x19;
            this.btnClose.Text = "Close";
            this.btnClose.UseVisualStyleBackColor = true;
            base.AutoScaleDimensions = new SizeF(6f, 13f);
            base.AutoScaleMode = AutoScaleMode.Font;
            base.ClientSize = new Size(290, 0x20b);
            base.Controls.Add(this.btnClose);
            base.Controls.Add(this.lvComputers);
            base.Controls.Add(this.label5);
            base.Controls.Add(this.txtMSI);
            base.Controls.Add(this.label4);
            base.Controls.Add(this.label3);
            base.Controls.Add(this.label2);
            base.Controls.Add(this.txtDomain);
            base.Controls.Add(this.txtPassword);
            base.Controls.Add(this.txtUsername);
            base.Controls.Add(this.cbCredentials);
            base.Controls.Add(this.btnInstall);
            base.Controls.Add(this.label1);
            base.Controls.Add(this.txtComputername);
            base.Name = "MSIDeploy";
            this.Text = "MSIDeploy";
            base.ResumeLayout(false);
            base.PerformLayout();
        }

        private void InstallPackage(string MSI, string Computername, string Domain, string Username, string Password, TimeSpan timeout)
        {
            MSIInstall install = new MSIInstall();
            install.ConnectRemoteComputer(this.txtComputername.Text, this.txtDomain.Text, this.txtUsername.Text, this.txtPassword.Text, TimeSpan.FromSeconds(10.0));
            install.InstallProduct(MSI, "", true);
        }
    }
}

